<?php
/**
 * NP_Service_Asirra response.
 *
 * @author Nikola Posa <posa.nikola@gmail.com>
 * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License
 */
class NP_Service_Asirra_Response
{
    const ASIRRA_VALIDATION = 'AsirraValidation';
    const RESULT_ELEMENT = 'Result';
    const DEBUG_ELEMENT = 'Debug';

    /**
     * Flag which indicates whether result is valid or not.
     * 
     * @var bool
     */
    protected $_result;

    /**
     * Debug string which contains error message.
     *
     * @var string
     */
    protected $_debug;

    /**
     * Constructor.
     *
     * @param string $result
     * @param string $debug
     * @return void
     */
    public function  __construct($result = null, $debug = null)
    {
        if ($result !== null) {
            $this->setResult($result);
        }

        if ($debug !== null) {
            $this->setDebug($debug);
        }
    }

    /**
     * Set the result.
     *
     * @param string $result
     * @return NP_Service_Asirra_Response
     */
    public function setResult($result)
    {
        if (preg_match('/^pass$/i', (string)$result)) {
            $this->_result = true;
        }
        else {
            $this->_result = false;
        }

        return $this;
    }

    /**
     * Get the result.
     *
     * @return bool
     */
    public function getResult()
    {
        return $this->_result;
    }

    /**
     * Set debug message.
     *
     * @param string $debug
     * @return NP_Service_Asirra_Response
     */
    public function setDebug($debug)
    {
        $this->_debug = (string)$debug;

        return $this;
    }

    /**
     * Get debug message.
     *
     * @return string
     */
    public function getDebug()
    {
        return $this->_debug;
    }

    /**
     * Tells whether Asirra validation was successful.
     *
     * @return bool
     */
    public function isValid()
    {
        return $this->getResult();
    }

    /**
     * Creates new instance of this class based on the
     * $response object.
     *
     * @param Zend_Http_Response $response
     * @return NP_Service_Asirra_Response
     */
    public static function fromHttpResponse(Zend_Http_Response $response)
    {
        $body = $response->getBody();

        if (($xml = simplexml_load_string($body)) === false) {
            require_once 'NP/Service/Asirra/Exception.php';
            throw new NP_Service_Asirra_Exception('Invalid HTTP response.');
        }

        if ($xml->getName() != self::ASIRRA_VALIDATION) {
            require_once 'NP/Service/Asirra/Exception.php';
            throw new NP_Service_Asirra_Exception('Invalid HTTP response.');
        }

        $resultElement = self::RESULT_ELEMENT;
        $debugElement = self::DEBUG_ELEMENT;

        if (!($result = $xml->$resultElement) || !($debug = $xml->$debugElement)) {
            require_once 'NP/Service/Asirra/Exception.php';
            throw new NP_Service_Asirra_Exception('Invalid HTTP response.');
        }

        return new self($result, $debug);
    }
}